'use strict'

var appDirectives = angular.module('appDirectives', []);

var EmailReg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
var NameReg = /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/; 
var PwdReg = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]+$/;

appDirectives.directive('postSingle',['$http', '$cookies', 'messageService', function($http, $cookies, messageService){
	return {
		scope: {},
		restrict: 'A',
		link: function (scope, ele, attrs){
            //获取评论
			$(ele).on('click', '.comments', function(e){
                if(!scope.$parent.isCollapsed && !scope.$parent.commentsLoaded){
                    var post = scope.$parent.post;
                    $http.post('/comments', {postId: post._id}).success(function (data){
                        scope.$parent.comments = data.msg;
                        //设置已经加载过评论，下次就不用加载了
                        scope.$parent.commentsLoaded = true;
                    });
                }
			});

            //评论
			$(ele).on('click', '.comment', function () {
				var content = scope.$parent.content;
				var inPrivate = scope.$parent.inPrivate;
				if(content){
					var post = scope.$parent.post;
					$http.post('/comment', {content: content, inPrivate: inPrivate, postId: post._id, postAuthor: post.userName}).success(function(data){
						if(data.code){
							messageService.addMessage(data.code, '评论成功！');
							post.comments++;
							scope.$parent.comments.push(data.comment);
							scope.$parent.content = '';
						} else {
                            messageService.addMessage(data.code, data.msg);
						}
					});

				} else {
                    messageService.addMessage(0, '请输入评论内容！');
				}
			});

			$(ele).on('click', '.deleteCommentButton', function(){
				if(window.confirm('你确定要删除吗？')) {
					var id = $(this).attr('deleteId');
					var comment = scope.$parent.comments[id];
                    var post = scope.$parent.post;
					$http.post('/deleteComment', {id: comment._id}).success(function (data) {
                        messageService.addMessage(data.code, data.msg);
						if(data.code){
							//删除成功后删掉浏览器端的评论
							scope.$parent.comments.splice(id, 1);
                            post.comments --;
						}
					});
				} else {
					return ;
				}
			});
		}
	};
}]);

appDirectives.directive('inputValid', function (){
	return {
		scope: {},
		restrict: 'A',
		controller: function($scope){

			this.validToInvalid = function (ele){
				ele.removeClass('ng-valid').addClass('ng-invalid');
			};

			this.invalidToValid = function (ele){
				ele.removeClass('ng-invalid').addClass('ng-valid');
			};
		},
		link: function (scope, ele, attrs, ctrl){

		}
	};
});

appDirectives.directive('email', function (){
	return {
		restrict: 'A',
		require: 'inputValid',
		link: function (scope, ele, attrs, inputValidCtrl){

			ele.bind('keyup', function (e){
				var email = e.target.value.replace(/ /g, '');

				if(!EmailReg.test(email)){

					inputValidCtrl.validToInvalid($(ele));
				   
				} else {

					inputValidCtrl.invalidToValid($(ele));

				}
			});
			
		}
	};
});

appDirectives.directive('username', function (){
	return {
		restrict: 'A',
		require: 'inputValid',
		link: function (scope, ele, attrs, inputValidCtrl){

			ele.bind('keyup', function (e){
				var username = e.target.value.replace(/ /g, '');

				if(!NameReg.test(username) || username.length > 8 || username.length < 2){

					inputValidCtrl.validToInvalid($(ele));
				   
				} else {

					inputValidCtrl.invalidToValid($(ele));

				}
			});	
		}
	};
});

appDirectives.directive('pwd', function (){
	return {
		restrict: 'A',
		require: 'inputValid',
		link: function (scope, ele, attrs, inputValidCtrl){

			ele.bind('keyup', function (e){
				var pwd = e.target.value.replace(/ /g, '');

				if(!PwdReg.test(pwd) || pwd.length > 16 || pwd.length < 6){

					inputValidCtrl.validToInvalid($(ele));
				   
				} else {

					inputValidCtrl.invalidToValid($(ele));

				}
			});	
		}
	};
});

appDirectives.directive('pwd1', function (){
	return {
		restrict: 'A',
		require: 'inputValid',
		link: function (scope, ele, attrs, inputValidCtrl){

			ele.bind('keyup', function (e){
				var pwd = $('#pwd').val().trim();
				var pwd1 = e.target.value.replace(/ /g, '');
				if(pwd1 != pwd){

					inputValidCtrl.validToInvalid($(ele));
				   
				} else {

					inputValidCtrl.invalidToValid($(ele));

				}
			});	
		}
	};
});